<?php
namespace app\daifast\controller;
use \think\Controller;
use \think\Db; 
use think\View;


class Product extends \think\Controller
{
    
     
  
    /**
     * 添加主菜单 
     */
    public function addMenu(){ 
        
        input('action')=='add' and model('Product')->addMenu(); 
        return $this->allFetch(get_defined_vars()); 
    }
    
    /**
     * 添加子菜单
     */
    public function addMenuSon(){
        
        input('action')=='add' and model('Product')->addMenuSon(); 
        input('id') and $id = input('id'); 
        return $this->allFetch(get_defined_vars());
    }
    
    
    /**
     * 第一步
     */
    public function stepOne(){
    	$step = 1; 
    	$id = input('id');
    	if($_POST){
    		//更新配置表
    		$updateArr ['type'] = input('type');
    		$updateArr ['tables'] = input('tableName');
    		$updateArr ['url'] = input('menuUrl');
    		$updateArr ['product'] = 3;
    		Db::table('sys_menu_item')->where('id', $id)->update($updateArr);
    		$this->showMsg("配置成功", url('product/stepTwo',array('isNew'=>1,'id'=>$id,'tableName'=>input('tableName'))));
    	} 
    	$tableArr = Db::query("show tables"); 
    	//获取子菜单属性
    	$menuInfo = Db::table('sys_menu_item')->where(['id'=>$id])->find(); 
    	
    	return $this->allFetch(get_defined_vars());
    	
    }
    
    /**
     * 第二步
     */
    public function stepTwo(){
    	$step = 2;
    	//初始化变量  
    	$id = input('id');
    	$reset = input('isNew'); 
    	if($_POST){
    		model('Product')->setConfig($_POST['data']); 
    		$this->showMsg("配置成功", url('product/stepThree',array('id'=>$id)));
    	} 
    	$reset =="1" and $colArr = Db::query("select * from information_schema.COLUMNS where table_schema='".config("database.database")."' and table_name='".input('tableName')."'");
    	$reset !="1" and $list = Db::table("sys_product")->where(['menu_son_id'=>$id ])->select();
    	 
    	return $this->allFetch(get_defined_vars());
    }
    
    
    /**
     * 列表预览
     */
    public function stepThree(){
    	$step = 3;
    	$id = input('id');
    	$tableInfo = Db::table("sys_menu_item")->where(['id'=>$id ])->find();
    	$colsArr = Db::table("sys_product")->where(['menu_son_id'=>$id ])->order("cols_order asc")->select();
    	$tdColsArr = array_column($colsArr,"cols_name"); 
    	
    	$data = Db::table($tableInfo['tables'])->paginate(8);
    	$listArr = $data->items();
    	$page = $data->render();
    	
    	return $this->allFetch(get_defined_vars());
    }
    
    
    /**
     * 文件检测是否存在
     */
    public function ajaxCheckFile(){
        $id = input('id');
        $tableInfo = Db::table("sys_menu_item")->where(['id'=>$id ])->find();
        $arr = explode('/', $tableInfo['url']);
        $existC = model('Product')->checkFile('control',$arr[0]);
        $existM = model('Product')->checkFile('model',$arr[0]);
        $existV = model('Product')->checkFile('view',$arr[0]);
        ($existC||$existM||$existV) and echojson('检测到已存在的文件', 1) or  echojson('文件都不存在', 2);
        
    	
    }
    
    
 
    
    
    
    
    
    
    
    
    
    /**
     * 列表代码生成
     */
    
    public function stepOver(){
    	 
        model('Product')->creatPhpCode(input('id'));
        model('Product')->creatHtmlCode(input('id'));
        model('Product')->updateStep(input('id'),1);
        echojson("代码已生成",1);
         
    }
    
    
    
    
    
    
    
    
}
